package com.le.tester.leetcode;

import com.aliyuncs.http.HttpResponse;

/**
 * createTime：2024/4/29 9:57
 * description：接雨水
 */
public class Trap {
    public int trap(int[] height) {

        int sum = 0;
        int len = height.length;
        int len1 = len - 1;

        for (int i = 1; i < len1; i++) {
            int maxLeft = 0;
            //左边最高
            for (int j = i - 1; j >= 0; j--) {
                if (height[j] > maxLeft) {
                    maxLeft = height[j];
                }
            }

            int maxRight = 0;
            for (int j = i + 1; j < len; j++) {
                if (height[j] > maxRight) {
                    maxRight = height[j];
                }
            }
            //找出最小的
            int min = Math.min(maxLeft, maxRight);

            if (min > height[i]) {
                sum = sum + (min - height[i]);
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
        new Trap().trap(arr);
    }
}
